package cn.mogugo.joke.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.mogugo.comm.BaseAction;
import cn.mogugo.joke.action.form.JokeWebForm;
import cn.mogugo.joke.model.JokeWeb;

public class JokeWebAction  extends BaseAction{
	/**
	 * 默认方法
	 */
	public ActionForward unspecified(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		return list(mapping,form,request,response);
	}
	
	
	public ActionForward list(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		List list = this.jokeWebManager.getAll();
		request.setAttribute("list", list);
		return mapping.findForward("list");
	}
	
	
	
	public ActionForward create(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		return mapping.findForward("edit");
	}
	
	
	public ActionForward edit(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		JokeWeb jw = (JokeWeb)this.baseDao.get(JokeWeb.class, Integer.valueOf(id));
		JokeWebForm jokeWebForm = (JokeWebForm)form;
		BeanUtils.copyProperties(jokeWebForm ,jw);
		

		return mapping.findForward("edit");
	}
	
	
	public ActionForward saveOrUpdate(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		JokeWebForm jokeWebForm = (JokeWebForm)form;
		JokeWeb jw;
		if(StringUtils.isNotBlank(jokeWebForm.getId())){
			jw = (JokeWeb)this.baseDao.get(JokeWeb.class, Integer.valueOf(jokeWebForm.getId()));
		}else{
			jw = new JokeWeb();
		}
		BeanUtils.copyProperties(jw , jokeWebForm);
		if(jw.getId() == 0){
			jw.setId(null);
		}
		this.baseDao.saveOrUpdateLog(jw);
		
		return mapping.findForward("tolist");
	}
	
	
	
	public ActionForward delete(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		this.baseDao.deleteObjectByIdLog(JokeWeb.class, Integer.valueOf(id));
		return mapping.findForward("tolist");
	}
	


	
	public ActionForward getJokeFromWeb(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		JokeWeb jw = (JokeWeb)this.baseDao.get(JokeWeb.class, Integer.valueOf(id));
		if(jw.getWeb_name().equals("ZOL")){
			Integer totalPage = jw.getTotal_page();
			for(int ii = 119 ; ii <= totalPage ; ii ++ ){
				this.jokeWebManager.saveJokeFromZOL(jw , ii);
				Thread.sleep(5000);
			}
		}else if(jw.getWeb_name().equals("haha365")){
			Integer totalPage = jw.getTotal_page();
			for(int ii = 555 ; ii < totalPage ; ii ++ ){
				this.jokeWebManager.saveJokeFromhaha365(jw , ii);
				Thread.sleep(5000);
			}
		}
		
		return mapping.findForward("tolist");
	}
	
}
